package com.pronhub.cloud.shop.web.advice;


import com.pronhub.shop.cloud.CodeMSg;
import com.pronhub.shop.cloud.CommonControllerAdvice;
import com.pronhub.shop.cloud.Result;
import com.pronhub.cloud.shop.msg.MemberServerCodeMsg;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
public class MemberServerControllerAdvice extends CommonControllerAdvice {
    //定义我们自己项目的异常
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Result handlerBussinessExction(BindException e){
        //获取错误的消息  这个是错误的消息
        String msg = e.getAllErrors().get(0).getDefaultMessage();
        //错误吗的绑定
        //500103参数校验消息: 手机格式不正确
        //500103参数校验消息: 密码不能为空
        //需要在替换中间额错误信息
        CodeMSg codeMSg =MemberServerCodeMsg.PARAM_ERROR;
        //设置参数  有占位符的存在 可以用这个工具来改变内容
        //codeMSg.setMsg(MessageFormat.format(codeMSg.getMsg(),msg));
        return Result.error(MemberServerCodeMsg.PARAM_ERROR.fillArgs(msg));
    }

}
